package com.ruoyi.record.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.company.domain.XlkjCompany;
import com.ruoyi.company.service.IXlkjCompanyService;
import com.ruoyi.house.domain.XlkjHouse;
import com.ruoyi.house.service.IXlkjHouseService;
import com.ruoyi.incident.domain.XlkjIncident;
import com.ruoyi.incident.service.IXlkjIncidentService;
import com.ruoyi.ini.domain.XlkjIni;
import com.ruoyi.ini.service.IXlkjIniService;
import com.ruoyi.place.domain.XlkjPlace;
import com.ruoyi.place.service.IXlkjPlaceService;
import com.ruoyi.population.domain.XlkjPopulation;
import com.ruoyi.population.service.IXlkjPopulationService;
import com.ruoyi.record.entity.RecordVo;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.utils.DictStrUtils;
import com.ruoyi.utils.UrlUtils;
import org.apache.poi.hssf.record.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 采集记录接口
 */
@RestController
@RequestMapping("/app/record")
public class RecordControllerApp extends BaseController {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private IXlkjPopulationService populationService;
    @Autowired
    private IXlkjHouseService houseService;
    @Autowired
    private IXlkjCompanyService companyService;
    @Autowired
    private IXlkjPlaceService placeService;
    @Autowired
    private IXlkjIniService iniService;

    @GetMapping("/list/{phone}/{status}")
    public TableDataInfo list(@PathVariable("phone") String phone, @PathVariable("status") String status){
        //获取用户id
        SysUser sysUser = sysUserService.selectUserByUserName(phone);
        Long userId = sysUser.getUserId();

        List<RecordVo> recordList = new ArrayList<>();

        XlkjPopulation population = new XlkjPopulation();
        population.setUserId(userId);
        if(!status.equals("null")){
            population.setStatus(status);
        }
        List<XlkjPopulation> xlkjPopulations = populationService.selectXlkjPopulationList(population);
        for(XlkjPopulation population1:xlkjPopulations){
            RecordVo recordVo = new RecordVo();
            recordVo.setType("1");
            recordVo.setId(population1.getId());
            recordVo.setImg(population1.getPhoto());
            recordVo.setAddress(population1.getAddress());
            recordVo.setDepartmentId(population1.getDepartmentId());
            recordVo.setStatus(population1.getStatus());
            recordVo.setCreateTime(population1.getCreateTime());
            recordList.add(recordVo);
        }

        XlkjHouse house = new XlkjHouse();
        house.setUserId(userId);
        if(!status.equals("null")){
            house.setStatus(status);
        }
        List<XlkjHouse> xlkjHouses = houseService.selectXlkjHouseList(house);
        for(XlkjHouse house1:xlkjHouses){
            RecordVo recordVo = new RecordVo();
            recordVo.setType("1");
            recordVo.setId(house1.getId());
            recordVo.setImg(house1.getImg());
            recordVo.setAddress(house1.getAddress());
            recordVo.setDepartmentId(house1.getDepartmentId());
            recordVo.setStatus(house1.getStatus());
            recordVo.setCreateTime(house1.getCreateTime());
            recordList.add(recordVo);
        }

        XlkjCompany company = new XlkjCompany();
        company.setUserId(userId);
        if(!status.equals("null")){
            company.setStatus(status);
        }
        List<XlkjCompany> xlkjCompanies = companyService.selectXlkjCompanyList(company);
        for(XlkjCompany company1:xlkjCompanies){
            RecordVo recordVo = new RecordVo();
            recordVo.setType("1");
            recordVo.setId(company1.getId());
            recordVo.setAddress(company1.getAddress());
            recordVo.setDepartmentId(company1.getDepartmentId());
            recordVo.setStatus(company1.getStatus());
            recordVo.setCreateTime(company1.getCreateTime());
            recordList.add(recordVo);
        }

        XlkjPlace place = new XlkjPlace();
        place.setUserId(userId);
        if(!status.equals("null")){
            place.setStatus(status);
        }
        List<XlkjPlace> xlkjPlaces = placeService.selectXlkjPlaceList(place);
        for(XlkjPlace place1:xlkjPlaces){
            RecordVo recordVo = new RecordVo();
            recordVo.setType("1");
            recordVo.setId(place1.getId());
            recordVo.setImg(place1.getImg());
            recordVo.setAddress(place1.getPlaceAddress());
            recordVo.setDepartmentId(place1.getDepartmentId());
            recordVo.setStatus(place1.getStatus());
            recordVo.setCreateTime(place1.getCreateTime());
            recordList.add(recordVo);
        }

        for(RecordVo recordVo: recordList){
            if(recordVo.getImg() != null){
                recordVo.setImg(UrlUtils.getUrl() + recordVo.getImg());
            }
        }

        return getDataTable(recordList);
    }

    @GetMapping("/list/ban/{deptId}/{type}")
    public TableDataInfo listBan(@PathVariable("deptId") String deptId, @PathVariable("type") Long type){

        List<SysDept> list = sysDeptService.selectDeptByParentId(Long.valueOf(deptId));
        String str = "(";
        for(int i =0;i<list.size();i++){
            str += list.get(i).getDeptId();
            if(i != list.size()-1){
                str += ",";
            }
        }
        str += ")";

        if(str.equals("()")){
            str = null;
        }

        if(type == 1){
            XlkjPopulation population = new XlkjPopulation();
            population.setStatus("1");
            population.setDepartmentList(str);
            List<XlkjPopulation> xlkjPopulations = populationService.selectXlkjPopulationList(population);
            for(XlkjPopulation population1:xlkjPopulations){
                if(population1.getPhoto() != null){
                    population1.setPhoto(UrlUtils.getUrl() + population1.getPhoto());
                }
                SysUser sysUser = sysUserService.selectUserById(population1.getUserId());
                if(!sysUser.getAvatar().equals("")){
                    sysUser.setAvatar(UrlUtils.getUrl() + sysUser.getAvatar());
                }
                population1.setSysUser(sysUser);
            }
            return getDataTable(xlkjPopulations);
        }else if(type == 2){
            XlkjHouse house = new XlkjHouse();
            house.setStatus("1");
            house.setDepartmentList(str);
            List<XlkjHouse> xlkjHouses = houseService.selectXlkjHouseList(house);
            for(XlkjHouse population1:xlkjHouses){
                if(population1.getImg() != null){
                    population1.setImg(UrlUtils.getUrl() + population1.getImg());
                }
                SysUser sysUser = sysUserService.selectUserById(population1.getUserId());
                if(!sysUser.getAvatar().equals("")){
                    sysUser.setAvatar(UrlUtils.getUrl() + sysUser.getAvatar());
                }
                population1.setSysUser(sysUser);
            }
            return getDataTable(xlkjHouses);
        }else if(type == 3){
            XlkjCompany company = new XlkjCompany();
            company.setStatus("1");
            company.setDepartmentList(str);
            List<XlkjCompany> xlkjCompanies = companyService.selectXlkjCompanyList(company);
            for(XlkjCompany population1:xlkjCompanies){
                SysUser sysUser = sysUserService.selectUserById(population1.getUserId());
                if(!sysUser.getAvatar().equals("")){
                    sysUser.setAvatar(UrlUtils.getUrl() + sysUser.getAvatar());
                }
                population1.setSysUser(sysUser);
            }
            return getDataTable(xlkjCompanies);
        }else if(type == 4){
            XlkjPlace place = new XlkjPlace();
            place.setStatus("1");
            place.setDepartmentList(str);
            List<XlkjPlace> xlkjPlaces = placeService.selectXlkjPlaceList(place);
            for(XlkjPlace population1:xlkjPlaces){
                if(population1.getImg() != null){
                    population1.setImg(UrlUtils.getUrl() + population1.getImg());
                }
                SysUser sysUser = sysUserService.selectUserById(population1.getUserId());
                if(!sysUser.getAvatar().equals("")){
                    sysUser.setAvatar(UrlUtils.getUrl() + sysUser.getAvatar());
                }
                population1.setSysUser(sysUser);
            }
            return getDataTable(xlkjPlaces);
        }

        return getDataTable(null);
    }

    @GetMapping("/list/banIncident/{deptId}/{type}")
    public TableDataInfo listBanIncident(@PathVariable("deptId") String deptId, @PathVariable("type") Long type){
        List<SysDept> list = sysDeptService.selectDeptByParentId(Long.valueOf(deptId));
        String str = "(";
        for(int i =0;i<list.size();i++){
            str += list.get(i).getDeptId();
            if(i != list.size()-1){
                str += ",";
            }
        }
        str += ")";

        if(str.equals("()")){
            str = null;
        }

        XlkjIni ini = new XlkjIni();
//        ini.setSss("sss");                      //查询待处理
        ini.setDepartmentList(str);
        if(type == 1){
            ini.setType("1");
        }else if(type == 2 ){
            ini.setType("2");
            List<XlkjIni> xlkjInis = iniService.selectXlkjIniList(ini);
            ini.setType("3");
            List<XlkjIni> xlkjInis1 = iniService.selectXlkjIniList(ini);
            xlkjInis.addAll(xlkjInis1);
            for(XlkjIni inis:xlkjInis){
                inis.setType(DictUtils.getDictLabel(DictStrUtils.INCIDENT_TYPE,inis.getType()));
                inis.setStatus(DictUtils.getDictLabel(DictStrUtils.INCIDENT_STATUS,inis.getStatus()));
            }
            return getDataTable(xlkjInis);
        }else if(type == 3){
            ini.setType("4");
        }
        List<XlkjIni> xlkjInis = iniService.selectXlkjIniList(ini);
        for(XlkjIni inis:xlkjInis){
            inis.setType(DictUtils.getDictLabel(DictStrUtils.INCIDENT_TYPE,inis.getType()));
            inis.setStatus(DictUtils.getDictLabel(DictStrUtils.INCIDENT_STATUS,inis.getStatus()));

        }
        return getDataTable(xlkjInis);
    }

}
